//
//  TTExistGoodsChooseHeaderView.m
//  TaoTuo
//
//  Created by apple on 2020/11/27.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTExistGoodsChooseHeaderView.h"
#import "TTCustomHeaderView.h"
#import "TTExistGoodsScreenTableViewCell.h"

@interface TTExistGoodsChooseHeaderView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) TTCustomHeaderView *headerView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSMutableDictionary *paramsDic;

@end

@implementation TTExistGoodsChooseHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.paramsDic = @{
            @"InsertSize" : @"",
            @"HandSize" : @"",
            @"BraceletSize" : @"",
            @"GoldColor" : @"",
            @"ProductGrade" : @"",
        }.mutableCopy;
        
    }
    return self;
}

- (void)filterParamsChanged
{
    NSString *conditionString = @"";
    for (NSString *key in self.paramsDic) {
        NSString *value = self.paramsDic[key];
        if (value.length > 0) {
            conditionString = [NSString stringWithFormat:@"%@ == '%@' AND %@", key, value, conditionString];
        }
    }
    if (conditionString.length > 5) {
        conditionString = [conditionString substringToIndex:conditionString.length - 5];
    }
    
    if (self.filterConditionChangedBlock) {
        self.filterConditionChangedBlock(conditionString);
    }
}

- (void)setModel:(TTXianhuoModel *)model
{
    _model = model;
    [self.dataArray removeAllObjects];
    
    @weakify(self)
    if (model.InsertSizeList.count > 0) {
        NSMutableArray *xkDataArray = [NSMutableArray array];
        for (NSString *str in model.InsertSizeList) {
            NSString *value = [str stringByReplacingOccurrencesOfString:@"分" withString:@""];
            value = [str stringByReplacingOccurrencesOfString:@"以下" withString:@""];
            LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:str value:value isEnable:YES];
            [xkDataArray addObject:itemModel];
        }
        LLScreenSectionModel *xkModel = [LLScreenSectionModel setModelWithSectionTitle:@"镶口" allowMulti:NO allowCancelSelect:YES dataArray:xkDataArray];
        xkModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
            @strongify(self)
            if (selectedItemArray.count > 0) {
                LLScreenItemModel *m = selectedItemArray.firstObject;
                [self.paramsDic setObject:m.title forKey:@"InsertSize"];
            }else {
                [self.paramsDic setObject:@"" forKey:@"InsertSize"];
            }
            [self filterParamsChanged];
        };
        [self.dataArray addObject:xkModel];
    }
    
    if (model.HandSizeList.count > 0) {
        NSMutableArray *xkDataArray = [NSMutableArray array];
        for (NSString *str in model.HandSizeList) {
            NSString *value = [str stringByReplacingOccurrencesOfString:@"#" withString:@""];
            LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:str value:value isEnable:YES];
            [xkDataArray addObject:itemModel];
        }
        LLScreenSectionModel *xkModel = [LLScreenSectionModel setModelWithSectionTitle:@"手寸" allowMulti:NO allowCancelSelect:YES dataArray:xkDataArray];
        xkModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
            if (selectedItemArray.count > 0) {
                LLScreenItemModel *m = selectedItemArray.firstObject;
                [self.paramsDic setObject:m.title forKey:@"HandSize"];
            }else {
                [self.paramsDic setObject:@"" forKey:@"HandSize"];
            }
            
            [self filterParamsChanged];
            
        };
        [self.dataArray addObject:xkModel];
    }
    
    
    if (model.BraceletSizeList.count > 0) {
        NSMutableArray *szDataArray = [NSMutableArray array];
        for (NSString *str in model.BraceletSizeList) {
            NSString *value = [str stringByReplacingOccurrencesOfString:@"m" withString:@""];
            LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:str value:value isEnable:YES];
            [szDataArray addObject:itemModel];
        }
        LLScreenSectionModel *szModel = [LLScreenSectionModel setModelWithSectionTitle:@"手镯" allowMulti:NO allowCancelSelect:YES dataArray:szDataArray];
        szModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
            if (selectedItemArray.count > 0) {
                LLScreenItemModel *m = selectedItemArray.firstObject;
                [self.paramsDic setObject:m.title forKey:@"BraceletSize"];
            }else {
                [self.paramsDic setObject:@"" forKey:@"BraceletSize"];
            }
            [self filterParamsChanged];
        };
        [self.dataArray addObject:szModel];
    }
    
    if (model.GoldColorList.count > 0) {
        NSMutableArray *csDataArray = [NSMutableArray array];
        for (NSString *str in model.GoldColorList) {
            LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:str value:str isEnable:YES];
            [csDataArray addObject:itemModel];
        }
        LLScreenSectionModel *csModel = [LLScreenSectionModel setModelWithSectionTitle:@"成色" allowMulti:NO allowCancelSelect:YES dataArray:csDataArray];
        csModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
            if (selectedItemArray.count > 0) {
                LLScreenItemModel *m = selectedItemArray.firstObject;
                [self.paramsDic setObject:m.title forKey:@"GoldColor"];
            }else {
                [self.paramsDic setObject:@"" forKey:@"GoldColor"];
            }
            
            [self filterParamsChanged];
        };
        [self.dataArray addObject:csModel];
    }
    
    if (model.ProductGradeList.count > 0) {
        NSMutableArray *zgDataArray = [NSMutableArray array];
        for (NSString *str in model.ProductGradeList) {
            NSString *value = [str isEqualToString:@"精工"] ? @"0" : @"1";
            LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:str value:value isEnable:YES];
            [zgDataArray addObject:itemModel];
        }
        LLScreenSectionModel *zgModel = [LLScreenSectionModel setModelWithSectionTitle:@"做工" allowMulti:NO allowCancelSelect:YES dataArray:zgDataArray];
        zgModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
            if (selectedItemArray.count > 0) {
                LLScreenItemModel *m = selectedItemArray.firstObject;
                [self.paramsDic setObject:m.value forKey:@"ProductGrade"];
            }else {
                [self.paramsDic setObject:@"" forKey:@"ProductGrade"];
            }
            
            [self filterParamsChanged];
        };
        [self.dataArray addObject:zgModel];
    }
    
    
    self.tableView.height = self.dataArray.count * 40;
   
    self.height = self.tableView.bottom + 5;
    
    [self.tableView reloadData];
    
}

- (void)setCustomModel:(TTCustomConditionModel *)customModel
{
    _customModel = customModel;
    self.headerView.customModel = customModel;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTExistGoodsScreenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}



- (TTCustomHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [[TTCustomHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.width, 130)];
        [self insertSubview:_headerView aboveSubview:self.tableView];
    }
    return _headerView;
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.headerView.bottom, self.width, 40) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 40;
        [_tableView registerClass:[TTExistGoodsScreenTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.tableFooterView = [UIView new];
        [self addSubview:_tableView];
        
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



@end
